package com.hds.modules.app.controller;

import com.google.code.kaptcha.Producer;
import com.hds.common.utils.CaptchaUtil;
import com.hds.common.utils.R;
import com.hds.common.validator.Assert;
import com.hds.modules.app.entity.SMSCaptcha;
import com.hds.modules.app.service.SMSCaptchaService;
import com.hds.modules.app.utils.SmsUtils;
import com.hds.modules.sys.entity.SysCaptchaEntity;
import com.hds.modules.sys.service.SysCaptchaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Author guonan
 * @Email guonan@yewcn.com
 * @Description
 * @Date 2018/6/12 10:35
 */
@RestController
@RequestMapping("/app/sms")
public class SmsController {

    @Autowired
    private SMSCaptchaService sMSCaptchaService;

    @Autowired
    SysCaptchaService sysCaptchaService;

    @Autowired
    private Producer producer;

    @Autowired
    private SmsUtils smsUtils;



    /**
     * 发送手机短信验证码
     * @param mobile 手机号
     * @param code 图形验证码
     * statusCode:160040 【短信】验证码超出同模板同号码天发送上限
     * @return
     */
    @GetMapping("send")
    public R sendSmsCaptcha(@RequestParam(value="mobile", required=false) String mobile,
                            @RequestParam(value="code", required=false) String code) {
        Assert.isBlank(mobile, "手机号不能为空");
        if(!CaptchaUtil.isMobileNumber(mobile)){
            return R.error("手机号错误");
        }
        Assert.isBlank(code, "图形验证码不能为空");
        Map<String, Object> pmap = new HashMap<>();
        pmap.put("code", code);
        SysCaptchaEntity kaptcha=null;
        List<SysCaptchaEntity> kaptchas = sysCaptchaService.selectByMap(pmap);
        if(kaptchas!=null && kaptchas.size()>0)kaptcha = kaptchas.get(0);
        if(kaptcha==null){
            return R.error("图形验证码错误或已过期"); //过期删除
        }
        sysCaptchaService.validate(kaptcha.getUuid(), kaptcha.getCode());
        Map<String, Object> map = sMSCaptchaService.getSMSCaptcha(mobile);
        String statusCode = map.get("smsStatusCode").toString();
        if(SMSCaptcha.Code.SUCCESS.getCode().equals(statusCode)){
            return R.ok(map).desc("发送成功");
        }

        if(SMSCaptcha.Code.MAXLIMIT.getCode().equals(statusCode)){
            return R.error("发送失败").desc("短信验证码发送已达每日上限");
        }
        return R.error("短信验证码发送异常");
    }



    /**
     * 验证手机验证码是否正确、是否过期
     * @param mobile 手机号码
     * @param captcha 验证码
     */
    @GetMapping("verify")
    public R verifyCaptcha(@RequestParam("mobile")String mobile, @RequestParam("captcha")String captcha) {
        Assert.isBlank(mobile, "手机号不能为空");
        Assert.isBlank(captcha, "验证码不能为空");
        if(!"".equals(mobile.trim()) && CaptchaUtil.isMobileNumber(mobile)){
            boolean b = sMSCaptchaService.verify(mobile, captcha);
            return b ? R.ok() : R.error("手机验证码错误或已过期");
        } else{
            return R.error("手机号错误");
        }

    }



}


